/*
 * _______________#########_______________________ 
 * ______________############_____________________ 
 * ______________#############____________________ 
 * _____________##__###########___________________ 
 * ____________###__######_#####__________________ 
 * ____________###_#######___####_________________ 
 * ___________###__##########_####________________ 
 * __________####__###########_####_______________ 
 * ________#####___###########__#####_____________ 
 * _______######___###_########___#####___________ 
 * _______#####___###___########___######_________ 
 * ______######___###__###########___######_______ 
 * _____######___####_##############__######______ 
 * ____#######__#####################_#######_____ 
 * ____#######__##############################____ 
 * ___#######__######_#################_#######___ 
 * ___#######__######_######_#########___######___ 
 * ___#######____##__######___######_____######___ 
 * ___#######________######____#####_____#####____ 
 * ____######________#####_____#####_____####_____ 
 * _____#####________####______#####_____###______ 
 * ______#####______;###________###______#________ 
 * ________##_______####________####______________ 
 * 
 * @Author: ipk518 121206530@qq.com
 * @Date: 2025-06-04 15:45:58
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-06-04 16:10:17
 * @FilePath: /etws-sxk2307/tools/gnss/gnss_data.c
 * @Description: 
 * 你把时间投在专业上，两三年，你就能在圈子里小有名气。四五年，你就能靠这个专业赚钱。过了10年以上，你就能成为这个领域的专家
 * Copyright (c) 2025 by etws@quyujiang, All Rights Reserved. 
 */

#include "gnss_data.h"

// void GNSS_DataDeal(unsigned char *addr_data, short unsigned int data_length,gnss_data_struct *gnss_data)
// {
//     char *addr_data_temp = (char *)addr_data;                   // 滑块位置
//     char *addr_data_temp_end = addr_data_temp + data_length; // 滑块终点
//     char TTSSS[6] = "TTSSS"; // NMEA Address

//     while (addr_data_temp <= addr_data_temp_end)
//     {
//         // nmea message
//         if (*addr_data_temp == 0x24) //"$"
//         {
//             memcpy(TTSSS, addr_data_temp + 1, 5);
//             if (strcmp(TTSSS, "GNGGA") == 0) // 识别到NMEA地址为 ? GNRMC”，滑块仍位于 ??$ ?????
//             {
//                 char *ppos;
//                 char *next_ppos;
//                 uint8_t data_mun;
//                 for (data_mun = 0; data_mun < GNSS_NMEA_GGA_DATA_BYTE; data_mun++) // 根据“GNRMC”确定了 ?????要解析的协议格式与滑块本次的移动次数
//                 {
//                     ppos = strchr(addr_data_temp, ',');
//                     if (ppos == NULL)
//                     {
//                         break;
//                     }
//                     else
//                     {
//                         switch (data_mun)
//                         {
//                         case 0:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(gnss_data->gngga.utc_time, ppos, next_ppos - ppos);
//                             break;
//                         case 1:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 2:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.latitude_latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 3:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.longitude), ppos, next_ppos - ppos);
//                             break;
//                         case 4:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.longitude_latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 5:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.quality), ppos, next_ppos - ppos);
//                             break;
//                         case 6:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.numSV), ppos, next_ppos - ppos);
//                             break;
//                         case 7:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.hdop), ppos, next_ppos - ppos);
//                             break;
//                         case 8:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.alt), ppos, next_ppos - ppos);
//                             break;
//                         case 9:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.altunit), ppos, next_ppos - ppos);
//                             break;
//                         case 10:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.sep), ppos, next_ppos - ppos);
//                             break;
//                         case 11:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.sepUnit), ppos, next_ppos - ppos);
//                             break;
//                         case 12:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.diffAge), ppos, next_ppos - ppos);
//                             break;
//                         case 13:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gngga.diffStation), ppos, next_ppos - ppos);
//                             break;
//                         default:
//                             ppos++;
//                             break;
//                         }
//                     }
//                     addr_data_temp = ppos; // 成功解析数据后滑块位 ?????
//                 }
//             }
//             else if (strcmp(TTSSS, "GNRMC") == 0) // 识别到NMEA地址为 ? GNRMC”，滑块仍位于 ??$ ?????
//             {
//                 char *ppos;
//                 char *next_ppos;
//                 uint8_t data_mun;
//                 for (data_mun = 0; data_mun < GNSS_NMEA_RMC_DATA_BYTE; data_mun++) // 根据“GNRMC”确定了 ?????要解析的协议格式与滑块本次的移动次数
//                 {
//                     ppos = strchr(addr_data_temp, ',');
//                     if (ppos == NULL)
//                     {
//                         break;
//                     }
//                     else
//                     {
//                         switch (data_mun)
//                         {
//                         case 0:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(gnss_data->gprmc.utc_time, ppos, next_ppos - ppos);
//                             break;
//                         case 1:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.location_status), ppos, next_ppos - ppos);
//                             break;
//                         case 2:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 3:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.latitude_latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 4:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.longitude), ppos, next_ppos - ppos);
//                             break;
//                         case 5:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.longitude_latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 6:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.speed), ppos, next_ppos - ppos);
//                             break;
//                         case 7:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.azimuth), ppos, next_ppos - ppos);
//                             break;
//                         case 8:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.utc_data), ppos, next_ppos - ppos);
//                             break;
//                         case 9:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.magnetic_declination), ppos, next_ppos - ppos);
//                             break;
//                         case 10:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.magnetic_declination_latitude), ppos, next_ppos - ppos);
//                             break;
//                         case 11:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.location_mode), ppos, next_ppos - ppos);
//                             break;
//                         case 12:
//                             ppos++;
//                             next_ppos = strchr(ppos, ',');
//                             memcpy(&(gnss_data->gprmc.navStatus), ppos, next_ppos - ppos);
//                             break;
//                         default:
//                             ppos++;
//                             break;
//                         }
//                     }
//                     addr_data_temp = ppos; // 成功解析数据后滑块位 ?????
//                 }
//             }
//             else
//             {
//             }
//         }
//         // ublox message
//         if ((*addr_data_temp == 0x0d) && (*(addr_data_temp + 1) == 0x0a) && (*(addr_data_temp + 2) == 0xb5) && (*(addr_data_temp + 3) == 0x62)) // 在每个NAME数据帧后判断UBX帧头0xb5 0x62
//         {
//             switch (*(addr_data_temp + 4)) // ublox class
//             {
//             case 0x01:                         // class = 0x01 :UBX-NAV
//                 switch (*(addr_data_temp + 5)) // ublox ID
//                 {
//                 case 0x3c: // ID = 0X01 :RELPOSNED
//                     memcpy(&(gnss_data->ubx_nav_relposned.relPosLength), addr_data_temp + 28, sizeof(gnss_data->ubx_nav_relposned.relPosLength));
//                     memcpy(&(gnss_data->ubx_nav_relposned.relPosHeading), addr_data_temp + 32, sizeof(gnss_data->ubx_nav_relposned.relPosHeading));
//                     memcpy(&(gnss_data->ubx_nav_relposned.accLength), addr_data_temp + 56, sizeof(gnss_data->ubx_nav_relposned.accLength));
//                     memcpy(&(gnss_data->ubx_nav_relposned.accHeading), addr_data_temp + 60, sizeof(gnss_data->ubx_nav_relposned.accHeading));
//                     break;
//                 default:
//                     break;
//                 }
//                 break;
//             default:
//                 break;
//             }
//         }
//         addr_data_temp++; // needle ++
//     }
//     gnss_data->gnss_data_output.herder = 0x55aa;
//     gnss_data->gnss_data_output.order_code = 0x01;
//     gnss_data->gnss_data_output.length = sizeof(gnss_data->gnss_data_output) - 7; // 0x003c;
//     memcpy(&(gnss_data->gnss_data_output.utc_time), &(gnss_data->gprmc.utc_time), sizeof(gnss_data->gprmc.utc_time));

//     gnss_data->gnss_data_output.location_status = gnss_data->gprmc.location_status;
//     memcpy(&(gnss_data->gnss_data_output.latitude), &(gnss_data->gprmc.latitude), sizeof(gnss_data->gprmc.latitude));

//     gnss_data->gnss_data_output.latitude_latitude = gnss_data->gprmc.latitude_latitude;
//     memcpy(&(gnss_data->gnss_data_output.longitude), &(gnss_data->gprmc.longitude), sizeof(gnss_data->gprmc.longitude));

//     gnss_data->gnss_data_output.longitude_latitude = gnss_data->gprmc.longitude_latitude;
//     memcpy(&(gnss_data->gnss_data_output.alt), &(gnss_data->gngga.alt), sizeof(gnss_data->gngga.alt));
//     memcpy(&(gnss_data->gnss_data_output.utc_data), &(gnss_data->gprmc.utc_data), sizeof(gnss_data->gprmc.utc_data));

//     gnss_data->gnss_data_output.relPosLength = gnss_data->ubx_nav_relposned.relPosLength;
//     gnss_data->gnss_data_output.accLength = gnss_data->ubx_nav_relposned.accLength;
//     gnss_data->gnss_data_output.relPosHeading = gnss_data->ubx_nav_relposned.relPosHeading;
//     gnss_data->gnss_data_output.accHeading = gnss_data->ubx_nav_relposned.accHeading;

//     gnss_data->gnss_data_output.crc = CRC16_Deal((uint8_t *)&(gnss_data->gnss_data_output.herder), sizeof(gnss_data->gnss_data_output) - 2);
// }

void GNSS_DataDeal(unsigned char *addr_data, unsigned int data_length)
{
	char *addr_data_temp;	  // 滑块位置
	char *addr_data_temp_end; // 滑块终点
	char TTSSS[5];			  // NMEA Address
  addr_data_temp = (char *)addr_data;                   //滑块位置
  addr_data_temp_end = addr_data_temp + data_length; //滑块终点
  char test002 = 1;
  while (addr_data_temp <= (addr_data_temp_end))
  {
    // nmea message
    if (*addr_data_temp == 0x24) //"$"
    {
      memcpy(TTSSS, addr_data_temp + 1, 5);
      test002 = strncmp(TTSSS, "GNGGA",5);//strcmp
      if (test002 == 0) //识别到NMEA地址为 ? GNRMC”，滑块仍位于 ??$ ?????
      {
        char *ppos;
        char *next_ppos;
        uint8_t data_mun;
        for (data_mun = 0; data_mun < GNSS_NMEA_GGA_DATA_BYTE; data_mun++) //根据“GNRMC”确定了 ?????要解析的协议格式与滑块本次的移动次数
        {
          ppos = strchr(addr_data_temp, ','); 
          if (ppos == NULL)
          {
            ppos++;
            data_mun++;
            break;
          }
          else
          {
            switch (data_mun)
            {
            case 0:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.utc_time), ppos, next_ppos - ppos);
              }
              break;
            case 1:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.latitude), ppos, next_ppos - ppos);
              }
              break;
            case 2:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.latitude_latitude), ppos, next_ppos - ppos);
              }
              break;
            case 3:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.longitude), ppos, next_ppos - ppos);
              }
              break;
            case 4:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.longitude_latitude), ppos, next_ppos - ppos);
              }
              break;
            case 5:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.quality), ppos, next_ppos - ppos);
              }
              break;
            case 6:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.numSV), ppos, next_ppos - ppos);
              }
              break;
            case 7:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.hdop), ppos, next_ppos - ppos);
              }
              break;
            case 8:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.alt), ppos, next_ppos - ppos);
              }
              break;
            case 9:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.altunit), ppos, next_ppos - ppos);
              }
              break;
            case 10:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.sep), ppos, next_ppos - ppos);
              }
              break;
            case 11:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.sepUnit), ppos, next_ppos - ppos);
              }
              break;
            case 12:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.diffAge), ppos, next_ppos - ppos);
              }
              break;
            case 13:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gngga.diffStation), ppos, next_ppos - ppos);
              }
              break;
            default:
              ppos++;
              break;
            }
          }
          addr_data_temp = ppos; //成功解析数据后滑块位 ?????
        }
      }
      else if (strncmp(TTSSS, "GNRMC",5) == 0)
      {
        char *ppos;
        char *next_ppos;
        uint8_t data_mun;
        for (data_mun = 0; data_mun < GNSS_NMEA_RMC_DATA_BYTE; data_mun++) //根据“GNRMC”确定了 ?????要解析的协议格式与滑块本次的移动次数
        {
          ppos = strchr(addr_data_temp, ','); 
          if (ppos == NULL)
          {
            ppos++;
            data_mun++;
            break;
          }
          else
          {
            switch (data_mun)
            {
            case 0:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.utc_time), ppos, next_ppos - ppos);// gnss_data.gprmc.utc_time = atof(ppos + 1);
              
              }
              break;
            case 1:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.location_status), ppos, next_ppos - ppos);
              }
              break;
            case 2:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.latitude), ppos , next_ppos - ppos);
              }
              break;
            case 3:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.latitude_latitude), ppos , next_ppos - ppos);
              }
              break;
            case 4:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.longitude), ppos, next_ppos - ppos);
              }
              break;
            case 5:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.longitude_latitude), ppos, next_ppos - ppos);
              }
              break;
            case 6:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.speed), ppos , next_ppos - ppos);
              }
              break;
            case 7:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.azimuth), ppos , next_ppos - ppos);
              }
              break;
            case 8:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.utc_data), ppos , next_ppos - ppos);
              }
              break;
            case 9:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.magnetic_declination), ppos , next_ppos - ppos);
              }
              break;
            case 10:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.magnetic_declination_latitude), ppos, next_ppos - ppos);
              }
              break;
            case 11:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.location_mode), ppos, next_ppos - ppos);
              }
              break;
            case 12:
              ppos++;
              next_ppos = strchr(ppos, ',');
              if (next_ppos - ppos > 0)
              {
                memcpy(&(gnss_data.gnrmc.navStatus), ppos, next_ppos - ppos);
              }
              break;
            default:
              ppos++;
              break;
            }
          }
          addr_data_temp = ppos; //成功解析数据后滑块位 ?????
        }
      }
      //else if (strncmp(TTSSS, "",5) == 0)
      //{
        /* code */
      //}
      else
      {
        /* code */
      }
    }
    // ublox message
    if ((*addr_data_temp == 0x0d) && (*(addr_data_temp + 1) == 0x0a) && (*(addr_data_temp + 2) == 0xb5) && (*(addr_data_temp + 3) == 0x62)) //在每个NAME数据帧后判断UBX帧头0xb5 0x62
    {
        switch (*(addr_data_temp + 4)) // ublox class
        {
        case 0x01:                       // class = 0x01 :UBX-NAV
          switch (*(addr_data_temp + 5)) // ublox ID
          {
          case 0x3c: // ID = 0X01 :RELPOSNED
            memcpy(&(gnss_data.ubx_nav_relposned.relPosLength), addr_data_temp + 28, sizeof(gnss_data.ubx_nav_relposned.relPosLength));
            memcpy(&(gnss_data.ubx_nav_relposned.relPosHeading), addr_data_temp + 32, sizeof(gnss_data.ubx_nav_relposned.relPosHeading));
            memcpy(&(gnss_data.ubx_nav_relposned.accLength), addr_data_temp + 56, sizeof(gnss_data.ubx_nav_relposned.accLength));
            memcpy(&(gnss_data.ubx_nav_relposned.accHeading), addr_data_temp + 60, sizeof(gnss_data.ubx_nav_relposned.accHeading));
            
            break;
          default:
            break;
          }
          break;
        default:
          break;
        }
    }
    addr_data_temp++; // needle ++
  }
}
